"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectProjectjkCtrl
 * @description
 * # ProjectProjectjkCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectProjectjkCtrl", function(
    $rootScope,
    $scope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);

    $scope.chooseProject = function() {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    // 选择项目收款
    $scope.chooseSk = function() {
      var params = $scope.param;
      if (!params.projectId) {
        Util.Toast("请先选择项目");
        return;
      }
      var projectParams = {
        projectId: params.projectId,
        formKey: "Xm_ProjectGathering",
        fieldName: "gatheringNo"
      };
      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };
    
    $scope.getborrowPurposeList = function() {
      var param = {};
      param.type = "项目借款类型";
      Util.getKeyValue(param, function(data) {
        $scope.borrowPurposeList = data.data;
      });
    };

    $scope.showSearchPicker = function() {
      $scope.searchtitle = "收款人";
      $scope.apiurl = openApi.projectborrowingHistory;
      $scope.showSearchInput = true;
      $rootScope.$emit("showmasker");
    };
    $scope.handleSearch = function(data) {
      $scope.param.receivePerson = data[0];
      $scope.param.receiveAccount = data[1];
      $scope.param.receiveBank = data[2];
      hideSearchPicker();
    };
    var hideSearchPicker = function() {
      $rootScope.$emit("closemasker");
      $scope.showSearchInput = false;
    };
    $scope.$on("closemasker", function() {
      hideSearchPicker();
    });

    $scope.submit = function(operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.projectborrowing;
      options.param = angular.copy($scope.param, {});
      FormSubmit.submit(options,$scope);
    };
    $scope.validate = function() {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目");
        return;
      }
      if (!$scope.param.borrowPurpose) {
        Util.Toast("请选择款项用途");
        return;
      }
      if (!$scope.param.borrowingAmount) {
        Util.Toast("请输入借款金额");
        return;
      }
      if (!$scope.param.borrowReason) {
        Util.Toast("请输入借款事由");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function() {
      // 获取回显的详细信息
      Util.getDetailData(id, function(data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
      });
      $scope.getborrowPurposeList();
    };
    // 初始化
    init();
  });
